package com.hospital;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class Hospital {
    public ArrayList<Department> departments = new ArrayList<>();
    public ArrayList<Schedule> schedules = new ArrayList<>();
    public Scanner sc = new Scanner(System.in);

    public void init() {

        while (true) {

            System.out.println("===============医院管理系统===============");
            System.out.println("输入序号选择功能");
            System.out.println("1.科室管理");
            System.out.println("2.医生管理");
            System.out.println("3.排班管理");

            int type = sc.nextInt();

            switch (type) {
                case 1:
                    this.manageDepartment();
                    break;
                case 2:
                    this.manageDoctor();
                    break;
                case 3:
                    this.manageSchedule();
                    break;

                default:
                    break;
            }
        }

    }

    public void manageSchedule() {
        Department department = this.selectDepartment();
        if (Objects.isNull(department)) {
            return;
        }

        Doctor doctor = selectDoctor(department);
        if (Objects.isNull(doctor)) {
            return;
        }

        this.updateSchedules(doctor);

    }

    public void updateSchedule(Doctor doctor) {

    }

    public void updateSchedules(Doctor doctor) {
        int DAYS = 7;
        ArrayList<Schedule> schedules = doctor.getSchedules();
        LocalDate now = LocalDate.now();
        if (schedules.size() == 0) {
            for (int i = 0; i < DAYS; i++) {
                Schedule schedule = new Schedule();
                schedule.setDate(now.plusDays(i));
                schedules.add(schedule);
            }

            return;
        }

        for (int i = 0; i < DAYS; i++) {
            if (schedules.get(i).getDate().isBefore(now)) {
                schedules.remove(i);
                i--;
            }
        }

        for (int index = 0; index < DAYS - schedules.size(); index++) {
            Schedule curSchedule = schedules.get(index);
            String tip = "请输入【" + doctor.getName() + "】医生" + curSchedule.getDate() + "的";
            System.out.println(tip + "信息，是否上午 （y/n）");
            boolean isMorning = sc.next().equals("y");

            curSchedule.setMorning(isMorning);
            if (!isMorning) {
                return;
            }
            System.out.println(tip + "上午接待人数");
            curSchedule.setaTotalNum(sc.nextInt());
            System.out.println(tip + "上午可预约人数");
            curSchedule.setmAppointNum(sc.nextInt());

            System.out.println(tip + "信息，是否下午 （y/n）");
            boolean isAfternoon = sc.next().equals("y");
            curSchedule.setAfternoon(isAfternoon);
            if (!isAfternoon) {
                return;
            }

            System.out.println(tip + "上午接待人数");
            curSchedule.setaTotalNum(sc.nextInt());
            System.out.println(tip + "上午可预约人数");
            curSchedule.setmAppointNum(sc.nextInt());

        }

    }

    public void manageDoctor() {

        while (true) {
            System.out.println("------------医生管理--------------");
            System.out.println("1.新增医生");
            System.out.println("2.删除医生");
            System.out.println("3.编辑医生");
            System.out.println("4.查看医生");
            System.out.println("0.返回上一级");
            int type = sc.nextInt();
            switch (type) {
                case 0:

                    return;
                case 1:
                    addDoctor();
                    break;
                case 4:
                    // showDoctors();
                    break;

                default:
                    System.out.println("输入有误");
                    break;
            }
        }

    }

    public void addDoctor() {

        Department department = this.selectDepartment();
        if (Objects.isNull(department)) {
            return;
        }

        String Pattern = "yyyy-MM-dd";
        System.out.println("输入医生姓名");
        String name = sc.next();
        System.out.println("输入入职时间：(" + Pattern + ")");
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(Pattern);
        String strDate = sc.next();
        LocalDate joinDate = LocalDate.parse(strDate, dtf);

        System.out.println("输入简介");

        String description = sc.next();

        Doctor doc = new Doctor(name,
                joinDate,
                description);
        department.addDoctor(doc);
    }

    public void manageDepartment() {

        while (true) {

            System.out.println("------------科室管理--------------");
            System.out.println("1.新增科室");
            System.out.println("2.删除科室");
            System.out.println("3.编辑科室");
            System.out.println("4.查看科室");
            System.out.println("0.返回上一级");
            int type = sc.nextInt();

            switch (type) {
                case 0:

                    return;
                case 1:
                    addDepartment();
                    break;
                case 4:
                    showAllDepartMent();
                    break;

                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    public void addDepartment() {

        OUT: while (true) {
            System.out.println("请输入科室名称");
            String name = sc.next();
            for (int i = 0; i < departments.size(); i++) {
                if (departments.get(i).getName().equals(name)) {
                    System.out.println("已存在重复科室名，请重新输入...");
                    continue OUT;
                }
            }
            Department department = new Department();
            department.setName(name);
            departments.add(department);
            break;
        }

    }

    public Doctor selectDoctor(Department department) {

        System.out.println("当前科室医生如下：");
        for (int i = 0; i < department.getDoctors().size(); i++) {
            Doctor curDoctor = department.getDoctors().get(i);
            System.out.println((i + 1) + "." + curDoctor.getName() + "--" + curDoctor.getDescription());
        }
        System.out.println("输入序号选择医生");

        int doctorNum = sc.nextInt();

        if (doctorNum <= 0 || doctorNum > department.getDoctors().size()) {
            System.out.println("输入序号有误");
            return this.selectDoctor(department);
        }

        Doctor targeDoctor = department.getDoctors().get(doctorNum - 1);
        return targeDoctor;
    }

    public Department selectDepartment() {
        if (departments.size() == 0) {
            System.out.println("请先添加科室");
            return null;
        }
        this.showAllDepartMent();
        int departmentNum = sc.nextInt();
        if (departmentNum <= 0 || departmentNum > departments.size()) {
            return this.selectDepartment();
        }
        return departments.get(departmentNum - 1);
    }

    public void showAllDepartMent() {
        System.out.println("所有科室如下：");
        for (int i = 0; i < departments.size(); i++) {
            System.out.println((i + 1) + "." + departments.get(i));
        }
    }

}
